package com.bjpowernode.springboot.web;

import com.bjpowernode.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @RequestMapping(value = "/each/list")
    public String list(Model model){
        List<User> userList = new ArrayList<>();
        for (int i=0; i<10; i++){
            User user = new User();
            user.setId(100+i);
            user.setName("张" + i);
            user.setAddress("郑州" + i);
            userList.add(user);
        }
        model.addAttribute("userList",userList);
        return "list";
    }

    @RequestMapping(value = "/each/map")
    public String map(Model model){
        Map<Integer,Object> userMap = new HashMap<>();
        for (int i=0; i<10; i++){
            User user = new User();
            user.setId(100+i);
            user.setName("张" + i);
            user.setAddress("郑州" + i);
            userMap.put(i,user);
        }
        model.addAttribute("userMap",userMap);
        return "map";
    }

    @RequestMapping(value = "/each/array")
    public String array(Model model){
        User[] users = new User[10];
        for (int i=0; i<10; i++){
            User user = new User();
            user.setId(100+i);
            user.setName("张" + i);
            user.setAddress("郑州" + i);
            users[i] = user;
        }
        model.addAttribute("users",users);
        return "users";
    }

    @RequestMapping(value = "/each/all")
    public String all(Model model){
        List<Map<Integer,List<User>>> userListMapList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            Map<Integer,List<User>> userListMap = new HashMap<>();
            for (int j = 0; j < 2; j++) {
                List<User> userList = new ArrayList<>();
                for (int k = 0; k < 3; k++) {
                    User user = new User();
                    user.setId(k*100+j*10+i);
                    user.setName("java"+k+j+i);
                    user.setAddress("java"+k+j+i);
                    userList.add(user);
                }
                userListMap.put(j,userList);
            }
            userListMapList.add(userListMap);
        }
        model.addAttribute("userListMapList",userListMapList);
        return "all";
    }
}
